package com.guigu.service.impl;

import com.guigu.dto.FollowUpDiaryDTO;
import com.guigu.mapper.FollowUpDiaryMapper;

import com.guigu.service.FileService;
import com.guigu.service.FollowUpDiaryService;
import com.guigu.vo.FollowUpDiaryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
@Service
public class FollowUpDiaryServiceImpl implements FollowUpDiaryService {

    @Autowired
    private FollowUpDiaryMapper followUpDiaryMapper;

    @Autowired
    private FileService fileService;

    @Override
    public List<FollowUpDiaryVO> FindFollowUpDiary(FollowUpDiaryDTO followUpDiaryDTO) {
        return this.followUpDiaryMapper.FindFollowUpDiary(followUpDiaryDTO);
    }

    @Override
    public FollowUpDiaryVO findIdByName(FollowUpDiaryDTO followUpDiaryDTO) {
        return this.followUpDiaryMapper.findIdByName(followUpDiaryDTO);
    }

    @Override
    @Transactional
    public void saveFollowUp(FollowUpDiaryDTO followUpDiaryDTO) {
        // 查询姓名对应的id
        FollowUpDiaryVO followUpDiaryVO = this.followUpDiaryMapper.queryIdByName(followUpDiaryDTO);
        followUpDiaryDTO.setIntervenedPerson(followUpDiaryVO.getId());

        // 合并时间类型
        LocalDateTime date = followUpDiaryDTO.getDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        LocalDateTime time = followUpDiaryDTO.getTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        LocalDateTime mergedDateTime = LocalDateTime.of(date.toLocalDate(), time.toLocalTime());

        // 将毫秒部分设置为0
        mergedDateTime = mergedDateTime.withNano(0);

        // 将 LocalDateTime 转换为 Timestamp
        Timestamp timestamp = Timestamp.valueOf(mergedDateTime);


        followUpDiaryDTO.setReturn_Visit_Date(timestamp);
        this.followUpDiaryMapper.saveFollowUp(followUpDiaryDTO);

    }

    @Override
    @Transactional
    public void saveFileInfo(FollowUpDiaryDTO followUpDiaryDTO) {
        this.followUpDiaryMapper.saveFile(followUpDiaryDTO);
        followUpDiaryDTO.setFile_id(followUpDiaryDTO.getF_id());
        saveFollowUp(followUpDiaryDTO);
    }

    @Override
    @Transactional
    public void delFollowById(FollowUpDiaryDTO followUpDiaryDTO) {
        FollowUpDiaryVO followUpDiaryVO = this.followUpDiaryMapper.queryFileById(followUpDiaryDTO);
        followUpDiaryDTO.setPath(this.followUpDiaryMapper.seeFileById(followUpDiaryVO.getFile_id()));
        if(followUpDiaryDTO.getPath()!=null){
           String info = this.fileService.deleteFile(followUpDiaryDTO.getPath());
           System.out.println(info);
        }
        this.followUpDiaryMapper.delFileById(followUpDiaryVO.getFile_id());
        this.followUpDiaryMapper.delFollowById(followUpDiaryDTO);


    }

    @Override
    public void updateFollowUpById(FollowUpDiaryDTO followUpDiaryDTO) {
        System.out.println(followUpDiaryDTO.getVisitId());
        // 合并时间类型
        LocalDateTime date = followUpDiaryDTO.getDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        LocalDateTime time = followUpDiaryDTO.getTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        LocalDateTime mergedDateTime = LocalDateTime.of(date.toLocalDate(), time.toLocalTime());

        // 将毫秒部分设置为0
        mergedDateTime = mergedDateTime.withNano(0);

        // 将 LocalDateTime 转换为 Timestamp
        Timestamp timestamp = Timestamp.valueOf(mergedDateTime);


        followUpDiaryDTO.setReturn_Visit_Date(timestamp);
        System.out.println(followUpDiaryDTO.getId());


        this.followUpDiaryMapper.updateFollowUpById(followUpDiaryDTO);
    }
}
